#include<stdio.h>
#include<unistd.h>

//myfile filename
int main(int argc,char* argv[])
{
  if(argc != 2)
  {
    printf("Usage: %s filename\n",argv[0]);
    return 1;
  }
  //chdir("/home/study/lesson"); //更改当前进程的工作路径，用于测试
  //1.默认这个文件会在哪里形成呢？ 当前路径（进程所在的路径）
  //2.关注一下文件清空的问题
  //3.测试其他操作：r,w,r+,w+,a,...
  //FILE *fp=fopen("log.txt","w"); //写入
  //FILE *fp=fopen("log.txt","a"); // a:追加操作
  //FILE *fp=fopen("log.txt","r");   //读入
  FILE *fp=fopen(argv[1],"r");   //读入
  if(fp == NULL)
  {
    perror("fopen");
    return 1;
  }

  //测试读取操作"r",fgets
  char buffer[64];
  while(fgets(buffer,sizeof(buffer),fp) != NULL)
  {
    //printf("echo: %s",buffer);
    printf("%s",buffer);
  }

  //printf("mypid: %d\n",getpid());
  //while(1)
  //{
  //  sleep(1);
  //}
  //const char* msg = "Test for  myfile";
  //int cnt =1;
  //while(cnt<=5)
  //{
  //  fprintf(fp,"%s: %d\n",msg,cnt++);
  //}
  fclose(fp);
}
